package com.echat.messagesender.sender.sms.impl.baseutil;

import com.echat.messagesender.sender.sms.bean.SmsWayConfig;
import com.echat.messagesender.sender.sms.bean.SmsWaySendResult;
import com.echat.messagesender.sender.sms.bean.smsway.SimpleXmlParserForWeiXin;
import com.echat.messagesender.sender.util.HttpUtil;
import com.echat.messagesender.sender.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Properties;

/**
 * Created by xiaoyu on 2016/2/2.
 * 欣易晨短信发送平台工具类
 */
public class SmsWayUtil {

    private static Logger logger = LoggerFactory.getLogger(AliSmsUtil.class);

    private interface Action {
        String SMSMessages = "/sendSMS.action";
    }

    private interface Parameter {
        String enterpriseID = "enterpriseID";
        String loginName = "loginName";
        String password = "password";
        String smsId = "smsId";
        String subPort = "subPort";
        String mobiles = "mobiles";
        String sendTime = "sendTime";
        String content = "content";
    }

    private static interface ResultStatusCode {
        int success = 0;
    }


    /**
     * 发送自定义内容的短信
     *
     * @param to         必选参数 短信接收端手机号码集合，用英文逗号分开，每批发送的手机号数量不得超过100个
     * @param content    必选参数 短信内容
     * @return
     */
    public static SmsWaySendResult sendSimpleSMS(SmsWayConfig config, String to,
                                                 String content) {
        return sendSMS0(config, to, content, null, null);
    }

    /**
     * 发送短信模板请求
     *
     * @param to         必选参数 短信接收端手机号码集合，用英文逗号分开，每批发送的手机号数量不得超过100个
     * @param templateId 必选参数 模板Id
     * @return
     */
    public static SmsWaySendResult sendTemplateSMS(SmsWayConfig config, String to,
                                                   String templateId, Map params) {
        return sendSMS0(config, to, null, templateId, params);
    }


    /**
     * 发送短信模板请求
     *
     * @param to         必选参数 短信接收端手机号码集合，用英文逗号分开，每批发送的手机号数量不得超过100个
     * @param templateId 必选参数 模板Id
     * @return
     */
    private static SmsWaySendResult sendSMS0(SmsWayConfig config, String to, String content,
                                             String templateId, Map params) {
        Map<String, Object> validate = null;
        String smsContent = content;
        if (StringUtils.isEmpty(smsContent)) {
            throw new IllegalArgumentException("短信内容(smsContent)或者模板ID(templateId) 其中必填1个");
        }
        String result = "";
        Properties parameters = new Properties();
        parameters.put(Parameter.enterpriseID, config.getEnterpriseID());
        parameters.put(Parameter.loginName, config.getLoginName());
        parameters.put(Parameter.password, config.getPassword());
        parameters.put(Parameter.mobiles, to);
        parameters.put(Parameter.content, smsContent);
        String requetUrl = getHttpRequestBaseUrl(config, Action.SMSMessages);
        try {
            result = HttpUtil.post(requetUrl, parameters);
            SmsWaySendResult smsWaySendResult = (SmsWaySendResult) SimpleXmlParserForWeiXin.parseXmlContentToBean(result, SmsWaySendResult.class, null);
            return smsWaySendResult;
        } catch (Exception e) {
            logger.error("SmsWayUtil 发送短信发生错误", e);
        }
        return new SmsWaySendResult();

    }

    private static String getHttpRequestBaseUrl(SmsWayConfig config, String action) {
        StringBuffer url = new StringBuffer(config.getApiBaseUrl()).append(action);
        return url.toString();
    }

}
